<?php
    class BlogPermalink extends Control {
	var $CurrentPage;
	var $DiscussionData;       
	
        function BlogPermalink(&$Context) {
	    $this->Name = 'BlogPermalink';
	    $this->Control($Context);      
            $this->CallDelegate('Constructor');
	}		
		
	function Render() {
	    $this->CallDelegate('PreRender');

            $DiscussionID = ForceIncomingInt('DiscussionID', 0);
	    if ($DiscussionID == 0) $DiscussionID = '';
            echo '<div class="ContentInfo Top"><h1>'.$this->Context->PageTitle.'</h1></div>';
            echo '<div id="ContentBody"><ol id="BlogList">';
           
            $BlogList = '';
            $ThemeFilePath = $this->Context->Configuration['EXTENSIONS_PATH'].'Blog/theme/Theme.BlogPermalink.php';
            $sql = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
            $sql->SetMainTable('Discussion','d');
            $sql->AddSelect(array('Name','AuthUserID','Closed','DateCreated','CountComments' ), 'd');
            $sql->AddWhere('d','DiscussionID','',$DiscussionID,'=');
            $result = $this->Context->Database->Select($sql, $this->Name, 'DiscussionStuff', 'An error occurred while listing the discussion information.');
            $Discussion = $this->Context->Database->GetRow($result);
            
            $sql->Clear();
            $sql->SetMainTable('User','u');
            $sql->AddSelect('Name', 'u');
            $sql->AddWhere('u','UserID','',$Discussion['AuthUserID'],'=');
            $result = $this->Context->Database->Select($sql, $this->Name, 'GetAuthorName', 'An error occurred while listing the discussion information.');
            $rows = $this->Context->Database->GetRow($result);
            $Discussion['AuthUsername'] = $rows[0];
            
            $sql->Clear();
            $sql->SetMainTable('Comment','c');
            $sql->AddSelect(array('Body', 'FormatType'), 'c');
            $sql->AddWhere('c','DiscussionID','',$DiscussionID,'=');
            $result = $this->Context->Database->Select($sql, $this->Name, 'GetCommentBody', 'An error occurred while listing the discussion information.');
            $rows = $this->Context->Database->GetRow($result);
            $CommentBody = str_replace("<!--more-->", "", $rows['Body']);
            $CommentBody = $this->Context->FormatString($CommentBody, $this, $rows['FormatType'], FORMAT_STRING_FOR_DISPLAY);

            include($ThemeFilePath); 
            $FirstRow = 0;
            
            echo $BlogList.'</ol></div>';
                echo '<div class="ContentInfo Bottom">
                <a class="left" href="'.GetUrl($this->Context->Configuration, 'extension.php', 'category/', 'CategoryID', $this->Context->Configuration['BLOG_CATEGORY_ID'], '',  'PostBackAction=Blog').'">'.$this->Context->GetDefinition('BackToBlog').'</a>
		<a id="TopOfPage" href="'.GetRequestUri().'#pgtop">'.$this->Context->GetDefinition('TopOfPage').'</a>
                </div>';

	}
        
    }
?>